package cn.meizhu.service.impl;

import cn.meizhu.client.AuthServiceClient;
import cn.meizhu.domain.Employee;
import cn.meizhu.domain.LoginUser;
import cn.meizhu.domain.Tenant;
import cn.meizhu.dto.LoginUserDto;
import cn.meizhu.dto.TenantEnteringDto;
import cn.meizhu.mapper.EmployeeMapper;
import cn.meizhu.mapper.TenantMapper;
import cn.meizhu.query.TenantQuery;
import cn.meizhu.service.ITenantService;
import cn.meizhu.util.util.AjaxResult;
import cn.meizhu.util.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author meizhu
 * @since 2022-04-05
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthServiceClient authServiceClient;

    @Override
    public PageList<Tenant> selectPageList(TenantQuery query, Page<Tenant> page) {
        List<Tenant> datas = tenantMapper.loadPageList(query,page);

        return new PageList<>(page.getTotal(),datas);
    }

    @Override
    @Transactional
    public void entering(TenantEnteringDto enteringDto) {
        //1 参数校验
        if (enteringDto == null){
            throw new RuntimeException("参数错误");
        }
        List<Tenant> tenants = tenantMapper.selectList(new EntityWrapper<Tenant>()
                .eq("company_name", enteringDto.getTenant().getCompanyName()));
        if (tenants != null && tenants.size() > 0){
            throw new RuntimeException("该机构已经入驻！请直接登录！");
        }
        //如果管理员已经存在，让他登录进去直接添加
        List<Employee> employees = employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("username", enteringDto.getEmployee().getUsername()));
        if (employees != null && employees.size() > 0){
            throw new RuntimeException("该管理员已经有机构了！请登录后添加新的机构!");
        }

        //2 远程调用auth feign接口添加loginUser维护套餐信息后返回loginId
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(enteringDto.getEmployee().getUsername());
        loginUser.setPassword(enteringDto.getEmployee().getPassword());
        loginUser.setType(0);

        LoginUserDto loginUserDto = new LoginUserDto();
        loginUserDto.setMealId(enteringDto.getMealId());
        loginUserDto.setLoginUser(loginUser);
        AjaxResult result = authServiceClient.add(loginUserDto);
        if (!result.isSuccess()){
            throw new RuntimeException("系统错误！");
        }
        Long loginUserId = Long.valueOf(result.getResultObj().toString());

        //3 添加管理员信息并返回adminId
        Employee employee = enteringDto.getEmployee();
        employee.setLoginId(loginUserId);
        employee.setInputTime(new Date());
        employee.setState(0);
        employeeMapper.insert(employee);

        //4 添加租户信息
        Long adminId = employee.getId();
        Tenant tenant = enteringDto.getTenant();
        tenant.setAdminId(adminId);
        tenant.setRegisterTime(new Date());
        tenantMapper.insert(tenant);

        employee.setTenantId(tenant.getId());
        employeeMapper.updateById(employee);

    }
}
